
/*
 * Copyright (C) 2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: SimpleIterator.java 206 2009-11-16 21:53:48Z archie.cobbs $
 */

package org.dellroad.sidekar.util;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Support superclass for simple {@link Iterator} implementations.
 */
public abstract class SimpleIterator<E> implements Iterator<E> {

    /**
     * Check for another element by invoking {@link #hasNext}, then return the result of delegating to {@link #getNext}.
     *
     * @throws NoSuchElementException if {@link #hasNext hasNext()} returns {@code false}
     */
    public final E next() {
        if (!hasNext())
            throw new NoSuchElementException();
        return getNext();
    }

    /**
     * This implementation always throws {@link UnsupportedOperationException}.
     *
     * @throws UnsupportedOperationException always
     */
    public void remove() {
        throw new UnsupportedOperationException();
    }

    /**
     * Get next element. This method may assume there is one (i.e., {@link #hasNext hasNext()} has just returned {@code true}).
     */
    protected abstract E getNext();
}

